<template>
  <div class="pagestyle" style="display: flex;">
    <div v-if="fromIndexpointId" style="width: 382px !important">
      <querybox :queryList="state.options.queryList.querys" @query="query"></querybox>
    </div>
    <div :style="{width:fromIndexpointId?'75%':'100%',height:fromIndexpointId?'72vh':'auto'}">
      <list_controller :showCaozuo="state.showCaozuo" @query="query" ref="zujianname" :options="state.options">
        <template v-slot:opraCurdBtnAll>
          <span :class="
                  state.listBtnType == 1 ? 'listInfoBtn active' : 'listInfoBtn'
                " @click="listInfoBtnClick(1)">
            <svg-icon :class="state.listBtnType == 1 ? 'active' : ''" class-name="search-icon" icon-class="qiehuan" />
            实时标校
          </span>
        </template>
        <template v-slot:selectNamelist> 历史标校 </template>
      </list_controller>
    </div>
  </div>
</template>
<script setup>
  import querybox from "@/components/commonCom/querybox";//用于首页抽屉查询处理
  import list_controller from "@/components/commonCom/list_controller";
  import {
    computed,
    nextTick,
    onMounted,
    reactive
  } from "vue";
  import {
    table_headers
  } from "@/components/commonCom/table_headers";
  import axios from "@/utils/request";
  import usePagesStore from "@/store/modules/pages";
  import {
    ElMessage,
    ElMessageBox,
    ElNotification
  } from "element-plus";
  import dayjs from "dayjs";
  import {
    searchareaTree
  } from "@/api/config";
  const {
    proxy
  } = getCurrentInstance();
  const store = usePagesStore();
  const router = useRouter();
  let previewHttps =
    import.meta.env.VITE_APP_HTTP;
  const upload = ref(null);
  const props = defineProps({
    fromIndexpointId: Object
  })
  let state = reactive({
    detailsShow: false,
    detailsList: [],
    tempList: [],
    fillayaComShow: false, //控制填写分析抽屉显示
    listBtnType: undefined,
    options: {
      tableheads: computed(() => table_headers.hisPointCalibration),
      queryList: {
        queryTitle: '选择测点',
        autoSearch:props.fromIndexpointId?true:undefined,
        querys: [{
            fieldsearch: "conditionType",
            field: "conditionType",
            title: "查询方式",
            type: "radiospecial",
            binddata: 1,
            options: [{
                label: "时间查询",
                value: 1,
              },
              {
                label: "班次查询",
                value: 2,
              },
            ],
          },
          {
            field: ["calibrationStartTime", "calibrationEndTime"],
            fieldsearch: ["calibrationStartTime", "calibrationEndTime"],
            title: "时间范围",
            col: 12,
            type: "datetimestartEnd",
            binddata: [dayjs().subtract(1, 'day').format('YYYY-MM-DD HH:mm:ss'), dayjs().format(
              'YYYY-MM-DD HH:mm:ss')],
            typespecial: 2,
            show: false,
          },
          {
            field: "datetime",
            fieldsearch: "datetime",
            title: "日期",
            type: "datetime",
            binddata: dayjs().format('YYYY-MM-DD'),
            typespecial: 1,
            show: true,
          },
          {
            fieldsearch: "banci",
            field: "banci",
            title: "班次选择",
            type: "selectspecial",
            binddata: "",
            options: [],
            typespecial: 1,
            show: true,
          },
          {
            field: "area",
            title: "所在区域",
            type: "cascaderRadionarmal",
            binddata: "",
            options: [],
            props: {
              children: "childList",
              value: "areaName",
              label: "areaName",
              checkStrictly: true,
              emitPath: false,
            },
          },
          {
            field: "condition",
            title: "测点类型",
            type: "cascaderRadio",
            binddata: "",
            options: [],
            props: {
              children: "children",
              value: "code",
              label: "name",
              checkStrictly: true,
              emitPath: false,
            },
          },
          {
            field: "pointId",
            title: "选择测点",
            type: "cascaderRadioSpecial",
            binddata: "",
            options: [],
            props: {
              children: "children",
              value: "commonField",
              label: "commonField",
              checkStrictly: false,
              emitPath: false,
            },
          },
        ],
      },
      dataUrl: "/hisPointCalibration/listHisPointCalibration",
    },
    editId: undefined,
    banciList: [],
    showCaozuo: true,
    data: {},
    areaObj: {
      condition: 'A',
      flag: "hisPointCalibration",
      isShowLocal: false
    },
    descObj: {},
    showChart: false,
    chartData: {},
  });

  function query(type) {
        if (type.type == 4) {
      state.options.queryList.querys.forEach((item) => {
        if (item.field == "condition") {
               if(type.val!=null){
            item.binddata = type.val;
            state.areaObj["condition"] = type.val;
          }else{
            state.areaObj["condition"] = 'A';
          }
        }
      });
      getconditionList(state.areaObj);
    }
  }
  function listInfoBtnClick(type) {
    state.listBtnType = type;
    if (type == 1) {
      goPage()
    }
    if (type == 2) {
      if (store.records.length == 0 || store.records.length > 1) {
        ElMessage.error('请选择一个测点')
        state.listBtnType = undefined;
        return
      } else {
        state.showChart = !state.showChart
        state.chartData = {
          pointIds: [store.records[0].pointId],
          queryStartTime: store.records[0].calibrationStartTime,
          queryEndTime: store.records[0].calibrationEndTime,
        }
      }
    }
  }
  // 获取分析模板
  function gettempList() {
    axios.post("/template/expression/obtainAll", {
      templateType: "A"
    }).then((res) => {
      state.tempList = res.data;
    });
  }
  gettempList();
  function getbanciList() {
    axios.post("/class/info/searchAll", {}).then((res) => {
      state.banciList = res.data.list;
      state.options.queryList.querys.forEach((item) => {
        if (item.fieldsearch == "banci") {
          item.options = res.data.list.map((item) => {
            return {
              label: item.className,
              value: item.id,
            };
          });
          item.binddata = item.options[0].value
        }
      });
    });
  }
  getbanciList();

  function banciList(dataObj) {
    axios.post("/hisPointFeed/handleClassInfo", dataObj).then((res) => {
      state.options.queryList.querys.forEach((item) => {
        if(dataObj.typeLabel!=2||state.options.queryList.querys[0].binddata==2){
          if (item.type == "datetimestartEnd") {
            item.binddata = [res.data.feedStartTime, res.data.feedEndTime];
          }
        }
      });
    });
  }

  function getSelList() {
    axios.post("/hisPointFeed/listAllNames", {
      flag: false
    }).then((res) => {
      state.options.queryList.querys.forEach((item) => {
        if (item.field == "condition") {
          let data = []
          res.data.forEach(item => {
            if (item.code != 'D') {
              data.push(item)
            }
          })
          item.options = data;
        }
      });
    });
    searchareaTree().then((res) => {
      state.options.queryList.querys.forEach((item) => {
        if (item.field == "area") {
          item.options = res.data;
        }
      });
    });
  }

  function getconditionList(dataObj) {
    state.options.queryList.querys.forEach((item) => {
      if (item.type == "datetimestartEnd") {
        dataObj.startTime = dayjs(item.binddata[0]).format('YYYY-MM-DD HH:mm:ss 000')
        dataObj.endTime = dayjs(item.binddata[1]).format('YYYY-MM-DD HH:mm:ss 000')
      }
    });
    axios
      .post("/hisPointFeed/conditionList", dataObj)
      .then((res) => {
        state.options.queryList.querys.forEach((item) => {
          if (item.type == "cascaderRadioSpecial") {
            item.options = res.data;
          }
        });
      });
  }
  getconditionList(state.areaObj)
  getSelList();
  // 跳转实时馈电异常页面
  const goPage = () => {
    router.push({
      name: "Lb_realCalibrationList",
    })
  }
 
  onMounted(() => {
    if (props.fromIndexpointId) {
      state.options.queryList.querys.forEach((item) => {
        item.col=24
        if (item.type == "cascaderRadioSpecial") {
          item.binddata = props.fromIndexpointId.pointId
          item.binddata1 = [props.fromIndexpointId.sensorName, props.fromIndexpointId.pointId, props
            .fromIndexpointId.place
          ]
          proxy.$refs['zujianname'].getData()
        }
      });

    }
  });
</script>
<style lang="scss" scoped>
  .xiazai {
    position: absolute;
    top: 42px;
    right: 110px;
    width: 120px;
    height: 64px;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: space-between;
    background-color: #ffff;
    box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
    border-radius: 2px;
    z-index: 99;

    .item {
      width: 100%;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }

    .item:hover {
      background-color: #ececec;
    }
  }

  ::v-deep .el-drawer {
    .querycss {
      .fsc {
        &:nth-child(2) {
          display: none;
        }
      }
    }

    .table_box {
      .fsc {
        display: none;
      }
    }
  }
</style>